function ProductListLoader() { }

ProductListLoader.prototype = {
    curProdPage: -1, // -1: end of list
    curQueryStr: null,
    isLoadTopScan: false,
    curPull: null,
    loadProducts: function (listview, pull, callback, message) {
        $(document).data('loading', true);
        BookmarksService.prototype.loadProductList(ProductListLoader.prototype.curQueryStr, 
        ProductListLoader.prototype.curProdPage, function(tr, results) {
        var len = results.rows.length;
        
        if (len == 0 && ProductListLoader.prototype.curProdPage == 0) {
            if (message == null) {
                monaca.updateUIStyle("footer", "visibility", "false");
                listview.append("<h3 stype='margin-top: 50px;'>表示するブックマークがありません</h3>").listview('refresh');
                $('.tutorial').show("slow");
            } else {
                listview.append("<h3 stype='margin-top: 50px;'>" + message + "</h3>").listview('refresh');
            }
            
            if (SearchService.prototype.bookmarkPullShow) {
                SearchService.prototype.bookmarkPullShow = false;
            }
        } else {
            monaca.retrieveUIStyle("checkallfooter", "text", function(value) {
                if (value != "すべて選択") {
                    $('.deleteCheck').prop('checked', false).checkboxradio('refresh');
                    monaca.updateUIStyle("checkallfooter", "text", "すべて選択");
                    monaca.updateUIStyle("deletefooter", "disable", "true");    
                }
            });
            
            var deleteDisplay = Util.prototype.isEditMode ? "display-OK" : "display-none",
            editDisplay = Util.prototype.isEditMode ? "display-inline" : "display-none";
                    
            var listHtml = "";
            for (i = 0; i < len; i++) {
                var item = results.rows.item(i);

                var titleClassName = (item.barcode_number != "") ? "itemName-barcode" : "itemName-search", // && item.barcode_number != "undefined" 
                dateText = item.registered_date.slice(0,4)+'/'+item.registered_date.slice(4,6)+'/'+item.registered_date.slice(6),
                editBtnCode = ProductListLoader.prototype.isLoadTopScan ? '' : '<td><a href="#" data-role="button" data-inline="true" data-mini="true" item-id="'+item.id+'" class="editList '+editDisplay+'" data-theme="a">編集</a></td>',
                bmDivided = '', 
                scan = '0'; /*0: bookmark delete, 1: scan delete, 2: bookmark update, 3: scan update*/
                if (ProductListLoader.prototype.isLoadTopScan) {
                    dateText = 'スキャンした日：' + dateText;
                    if (item.status == 2) {
                        bmDivided = ' productListBm ';
                        scan = '2';
                    } else {
                        scan = '1';
                    }
                }
                
                listHtml += '<table class="productList listview-item'+bmDivided+'" id="bookmarkTable'+item.id+'">'+
                                '<tr><td rowspan="3" class="deleteList '+deleteDisplay+'"><label style="position:relative; right:-0.3em" data-iconpos="notext">'+
                                        '<input type="checkbox" name="deleteCheck" status="'+scan+'" value="'+item.id+'" class="deleteCheck" data-theme="a" /></label></td>'+
                                    '<td colspan="2" class="'+titleClassName+'"><div style="overflow: hidden;">'+item.item_name+'</div>'+'</td></tr>'+
                                '<tr><td rowspan="2" class="itemImage" style="background-image:url('+item.image_url+');"></td>'+editBtnCode+'</tr>'+
                                '<tr><td class="itemNameDate"><p class="genreName">'+item.name+'</span></p><p class="registered">'+dateText+'</p></td></tr></table>';                     
            }
            listview.append(listHtml);
            $(".editList").button();
            $(".deleteList").trigger("create");
            listview.listview('refresh');            
        }
        
        var curPull = pull;
        if (curPull == null) {
            curPull = ProductListLoader.prototype.curPull;
        } else {
            ProductListLoader.prototype.curPull = curPull;
        }
        
        if (len < Util.prototype.maxBookmarkNumber) {
            ProductListLoader.prototype.curProdPage = -1;
            if (SearchService.prototype.bookmarkPullShow) {
                SearchService.prototype.bookmarkPullShow = false;
            }
            curPull.hide();
        } else {
            ProductListLoader.prototype.curProdPage ++;
            if (!(curPull.is(":visible"))){
                curPull.show();
            }    
        }

        $(document).data('loading', false);
        if (callback != null) {
            callback();
        } else {
            Util.prototype.hideSpinner();   
        }
        }, function(tr, error) {
            $(document).data('loading', false);
            Util.prototype.hideSpinner();
        });
    },
    
    initLoadTopBookMark: function() {
        ProductListLoader.prototype.isLoadTopScan = false;
        ProductListLoader.prototype.curProdPage = 0;
        ProductListLoader.prototype.curQueryStr = BookmarksService.prototype.loadTopBookmarkListQuery();
    },
    
    initLoadTopScan: function() {
        ProductListLoader.prototype.isLoadTopScan = true;
        ProductListLoader.prototype.curProdPage = 0;
        ProductListLoader.prototype.curQueryStr = BookmarksService.prototype.loadTopScanQuery();
    },
    
    initLoadRelativeItemSearch: function(keyword) {
        ProductListLoader.prototype.curProdPage = 0;
        SearchService.prototype.bookmarkPullShow = true;
        ProductListLoader.prototype.curQueryStr = BookmarksService.prototype.loadRelativeItemSearchQuery(keyword);
    },
    
    initLoadCityBookmark: function(cityCode) {
        ProductListLoader.prototype.isLoadTopScan = false;
        ProductListLoader.prototype.curProdPage = 0;
        ProductListLoader.prototype.curQueryStr = BookmarksService.prototype.loadCityBookmarkQuery(cityCode);
    },
    
    initLoadBookmarkList: function(registered, params) {
        ProductListLoader.prototype.isLoadTopScan = false;
        ProductListLoader.prototype.curProdPage = 0;
        ProductListLoader.prototype.curQueryStr = BookmarksService.prototype.getConditionBookmarksSQL(registered, params);
    },
    
    resetPage: function() {
        ProductListLoader.prototype.curProdPage = 0;
    }
};